#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int T,a[N],n,m,c[N],k,x0,x1,x2,x3;
int main()
{
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)scanf("%d",a+i);
    for(int i=1;i<=m;i++)scanf("%d",c+i);
    while(T--)
    {
        scanf("%d%d%d%d",&x0,&x1,&x2,&x3);
        printf("%d\n",((x0+x1+x3-x2)&a[1]%c[0]+x2-a[0])^m);
    }
    return 0;
}
//Aelena or Arena?That's quite a mystery.Arena maybe.
//But perhaps I can achieve my success not in Information Technology,but in archiology instead.
//History too.
//I love history and Information Technology,but I don't no which to choose.
//What should I do??????????????????????????????
//Aelena or Arena?That's quite a mystery.Arena maybe.
//But perhaps I can achieve my success not in Information Technology,but in archiology instead.
//History too.
//I love history and Information Technology,but I don't no which to choose.
//What should I do??????????????????????????????
//Aelena or Arena?That's quite a mystery.Arena maybe.
//But perhaps I can achieve my success not in Information Technology,but in archiology instead.
//History too.
//I love history and Information Technology,but I don't no which to choose.
//What should I do??????????????????????????????
//I will go to wu yi xiang and jiang nan gong yuan soon.
//Yahoo~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//(perhaps I'm a bit of crazy...)(maybe it is true)
//I will go to wu yi xiang and jiang nan gong yuan soon.
//Yahoo~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//(perhaps I'm a bit of crazy...)(maybe it is true)
//I will go to wu yi xiang and jiang nan gong yuan soon.
//Yahoo~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//(perhaps I'm a bit of crazy...)(maybe it is true)
//I will go to wu yi xiang and jiang nan gong yuan soon.
//Yahoo~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//(perhaps I'm a bit of crazy...)(maybe it is true)
